﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace KiraSh.ResumeTracker
{

    public partial class frmSelectProfile : Form
    {
        // parent
        public frmMainWindow form;


        public frmSelectProfile()
        {
            InitializeComponent();
            this.Text = CurrentContext.Name;
            // load list of profiles to drop box
            ListProfiles();

        }

        // update the list of profiles
        private void ListProfiles()
        {
            // load profile list
            ProfileManager.LoadFromDisk();
            // clean the drop box to make sure that there are no repeats
            bxSelectProfile.Items.Clear();
            // update the drop box and a copy of the profiles list to eliminate duplicates
            foreach (Profile pr in ProfileManager.Profiles)
            {
                if (pr.Name != null)
                {
                    bxSelectProfile.Items.Add(pr);
                }
            }
        }

        // add new profile to the xml
        private void btnAdd_Click(object sender, EventArgs e)
        {
            // get name of the new profile
            string selection = txtNewProfile.Text.Trim();
            try
            {
                // create new profile
                Profile newProfile = ProfileManager.CreateNewProfile(selection);
                // update/create profile file
                ProfileManager.SaveToDisk();
                // set current profile into context
                CurrentContext.Profile = newProfile;
                // show companies associated with profile
                SetMainForm();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // return to the main page aka the page 
        // that displays the company list
        private void SetMainForm()
        {
            form.Show();
            this.DialogResult = DialogResult.OK;
        }

        // profile was selected
        private void bxSelectProfile_SelectedIndexChanged(object sender, EventArgs e)
        {

            // update selected profile
            CurrentContext.Profile = (Profile)bxSelectProfile.SelectedItem;
            SetMainForm();
        }

        // if the form was terminated then close all windows else just close this one
        private void frmSelectProfile_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult != DialogResult.OK)
            {
                this.DialogResult = DialogResult.Abort;
            }
        }

    }
}
